<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

define('IN_AJAX', TRUE);

class c_ajax_class extends AWS_CONTROLLER
{
	public function get_access_rule()
	{
		$rule_action["rule_type"] = 'white'; //黑名单,黑名单中的检查  'white'白名单,白名单以外的检查
		
		$rule_action["actions"] = array(
			'check_username',
			'check_email',
			'register_process',
			'login_process',
			'register_agreement',
			'send_valid_mail',
			'valid_email_active',
			'request_find_password',
			'find_password_modify'
		);
		
		return $rule_action;
	}
	
	function setup()
	{
		HTTP::no_cache_header();
	}
	
	function check_username_action()
	{
		if (!$_GET['username'])
		{
			die('fail');
		}
		
		if (!$this->model('account')->check_username_sensitive_words($_GET['username']))
		{
			if (!$this->model('account')->check_username($_GET['username']))
			{
				die('availability');
			}
		}
		
		die('fail');
	}
	
	function check_email_action()
	{
		if (!$_GET['email'])
		{
			die('fail');
		}
		
		if (!$this->model('account')->check_email($_GET['email']))
		{
			die('availability');
		}
		
		die('fail');
	}
	
	function register_process_action()
	{
		if ($_POST['invite_question_id'])
		{
			$objetc_type = 1;
			$objetc_id = $_POST['invite_question_id'];
		}
		
		$__source = $_POST["source"];
		
		if (HTTP::get_cookie('source'))
		{
			$__source = HTTP::get_cookie('source');
		}
		
		$source = H::decode_hash($__source);
		
		if ($__source and $this->model('account')->check_email($source['email']))
		{
			// 有来源的用户无需邀请码
		}
		else
		{
			//邀请码验证
			$icode = trim($_POST["icode"]);
			
			if ($icode)
			{
				//检验失败
				if ($invitation = $this->model('invitation')->check_code_available($icode))
				{
					if (trim($_POST['email']) == $invitation['invitation_email'])
					{
						$email_valid = true;
					}
					else
					{
						H::ajax_json_output(AWS_APP::RSM(array(
						'input' => 'icode'
						), -1, "邀请码出错"));
					}
				}
				else
				{
					H::ajax_json_output(AWS_APP::RSM(array(
						'input' => 'icode'
					), -1, "邀请码出错"));
				}
			}
			else
			{
				if(get_setting('invite_reg_only') == 'Y')
				{
					H::ajax_json_output(AWS_APP::RSM(array(
					), -1, "本站只能通过邀请注册"));
				}
			}
		}
		
		//检查用户名
		if (trim($_POST["user_name"]) == '')
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'user_name'
			), -1, "请输入真实姓名"));
		}
		else if ($this->model('account')->check_username($_POST["user_name"]))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'user_name'
			), -1, "真实姓名已经存在"));
		}
		else if (! $this->model('account')->check_username_char($_POST["user_name"]))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'user_name'
			), -1, "真实姓名请输入 2-7 个汉字，或 4-14 个英文"));
		}
		else if ($this->model('account')->check_username_sensitive_words($_POST["user_name"]))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'user_name'
			), -1, "真实姓名中包含敏感词或系统保留字"));
		}
		
		if ($this->model('account')->check_email($_POST['email']))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'email'
			), -1, "E-Mail 已经被使用, 或格式不正确"));
		}
		
		if (strlen($_POST["password"]) < 6 OR strlen($_POST["password"]) > 16)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'userPassword'
			), -1, "密码长度不符合规则"));
		}
		
		if (! $_POST["agreement_chk"])
		{
			H::ajax_json_output(AWS_APP::RSM(null, -1, "请选择同意用户协议中的条款"));
		}
		
		// 检查验证码
		if (!($_POST['source'] || $_POST['icode']) && (!core_captcha::validate($_POST['seccode_verify'], false)) && (get_setting('register_seccode') == 'Y'))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'seccode_verify'
			), -1, '请填写正确的验证码'));
		}
		
		core_captcha::clear();

		if ($__source and $this->model('account')->check_email($source['email']))
		{
			// 有来源的用户无邀请码
			$follow_users = $this->model('account')->get_users_by_email($source['email']);
		}
		else
		{
			$follow_users = $this->model('invitation')->get_invitation_by_code($icode);
		}
		
		if ($follow_users)
		{
			$follow_uid = $follow_users["uid"];
		}
		
		if (get_setting('ucenter_enabled') == 'Y')
		{
			$result = $this->model('ucenter')->register($_POST['user_name'], $_POST['password'], $_POST['email'], $email_valid);
			
			if (is_array($result))
			{				
				$uid = $result['user_info']['uid'];
			}
			else
			{
				H::ajax_json_output(AWS_APP::RSM(null, -1, $result));
			}
		}
		else
		{
			$uid = $this->model('account')->user_register($_POST['user_name'], $_POST['password'], $_POST['email'], $email_valid);
		}
		
		if ($uid)
		{
			$this->model("account")->setcookie_logout(); // 清除COOKIE
			$this->model("account")->setsession_logout(); // 清除session;
			
			//$_SESSION["error"] = 0; // 重置登录出错计数
			
			if ($__source and $this->model('account')->check_email($source['email']) and $follow_uid)
			{
				// 有来源的用户自动产生邀请码
				$invitation_code = $this->model('invitation')->get_unique_invitation_code();
				
				$invitation_id = $this->model('invitation')->add_invitation($follow_uid, $invitation_code, $_POST['email'], time(), ip2long('127.0.0.1'));
				
				$icode = $invitation_code;
			}
			
			// 发送邀请问答站内信
			if ($objetc_id and $follow_users)
			{
				if ($objetc_type == 1)
				{
					$url = get_setting('base_url') . '/' . G_INDEX_SCRIPT . 'question/' . $_POST['invite_question_id'];
					
					$title = $follow_users['user_name'] . ' 邀请你来回复问题';
					$content = $follow_users['user_name'] . "  邀请你来回复问题: " . $url . " \r\n\r\n 邀请你来回复问题期待您的回复";
				}
				
				$this->model('message')->send_message($follow_uid, $uid, $title, $content, 0, 0);
			}
			
			//互为关注
			if ($follow_uid)
			{
				if ($this->model('follow')->user_follow_add($uid, $follow_uid))
				{
					$this->model('follow')->user_fans_count_edit($follow_uid, 1); //粉丝加1			
					$this->model('follow')->user_friend_count_edit($uid, 1); //我的关注会加1
					
					//互加
					$this->model('follow')->user_follow_add($follow_uid, $uid);
					$this->model('follow')->user_fans_count_edit($uid, 1);
					$this->model('follow')->user_friend_count_edit($follow_uid, 1);
				}
			}

			if ($icode || (get_setting('register_email_reqire') == 'N'))
			{
				$user_info = $this->model('account')->get_users_by_uid($uid);
				
				$this->model('account')->setcookie_login($user_info['uid'], $user_info['user_name'], $_POST['password'], $user_info['salt']);
						
				if ($icode)
				{
					$this->model('invitation')->invitation_code_active($icode, time(), real_ip(), $uid);
				}

				H::ajax_json_output(AWS_APP::RSM(array(
					'url' => get_setting('base_url') . '/' . G_INDEX_SCRIPT . 'home/first_login-TRUE'
				), 1, null)); // 返回数据
			}
			else
			{
				$_SESSION['valid_email'] = $_POST['email'];
				
				H::ajax_json_output(AWS_APP::RSM(array(
					'url' => get_setting('base_url') . '/' . G_INDEX_SCRIPT . 'account/valid_email/'
				), 1, null)); // 返回数据
			}
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, -1, '用户注册失败, 请联系管理员')); //返回数据			
		}
	}
	
	function login_process_action()
	{
		$url = $_POST['url'];
		
		$user_name = trim($_POST['user_name']);
		$password = $_POST['password'];
		$net_auto_login = intval($_POST['net_auto_login']);
		
		if (get_setting('ucenter_enabled') == 'Y')
		{
			if (!$user_info = $this->model('ucenter')->login($user_name, $password))
			{
				$user_info = $this->model('account')->check_login($user_name, $password);			
			}
		}
		else
		{
			$user_info = $this->model('account')->check_login($user_name, $password);
		}
		
		if (! $user_info)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'login_message'
			), -1, '请输入正确的帐号或密码'));
		}
		else
		{			
			if ($user_info['forbidden'] == 1)
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'tips_id' => 'login_message'
				), -1, '抱歉, 你的账号已经被禁止登录'));
			}
			
			// !注: 来路检测后面不能再放报错提示
			if (!valid_post_hash($_POST['post_hash']))
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'tips_id' => 'login_message',
				), -1, '表单来路不正确, 请刷新页面重试'));
			}

			if (!$user_info['valid_email'] && (get_setting('register_email_reqire') == 'Y'))
			{
				$_SESSION['valid_email'] = $user_info['email'];
				
				H::ajax_json_output(AWS_APP::RSM(array(
					'url' => get_js_url('/account/valid_email/')
				), 1, null));
			}
			
			if ($net_auto_login)
			{
				$expire = 60 * 60 * 24 * 360;
			}

			$this->model('account')->update_user_last_login($user_info['uid']);
			$this->model('account')->setcookie_logout();
			
			// 默认记住用户名
			HTTP::set_cookie('r_uname', $user_name, time() + 60 * 60 * 24 * 30);
			
			$this->model('account')->setcookie_login($user_info['uid'], $user_name, $password, $user_info['salt'], $expire);
			
			if ($user_info['is_first_login'])
			{
				$url = get_js_url('/home/first_login-TRUE');
			}
			
			if ($_POST['return_url'])
			{
				$url = $_POST['return_url'];
			}
			
			if (get_setting('ucenter_enabled') == 'Y')
			{
				$url = get_js_url('/account/sync_login/url-' . @base64_encode($url));
			}
			
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => $url
			), 1, null));
		}
	}
	
	public function register_agreement_action()
	{
		H::ajax_json_output(AWS_APP::RSM(null, 1, '<div style="width: 790px; height: 320px; overflow:auto;">' . nl2br(get_setting('register_agreement')) . '</div>'));
	}
	
	public function weibo_bind_action()
	{
		if (get_setting('sina_weibo_enabled') == 'Y')
		{
			if ($sina_weibo = $this->model("sina_weibo")->get_users_sina_by_uid($this->user_id))
			{
				$data['sina_weibo']['name'] = $sina_weibo['name'];
			}
		}
		
		if (get_setting('qq_t_enabled') == 'Y')
		{
			if ($qq_weibo = $this->model("qq_weibo")->get_users_qq_by_uid($this->user_id))
			{
				$data['qq_weibo']['name'] = $qq_weibo['name'];
			}
		}
		
		$data['sina_weibo']['enabled'] = get_setting('sina_weibo_enabled');
		$data['qq_weibo']['enabled'] = get_setting('qq_t_enabled');
		
		H::ajax_json_output(AWS_APP::RSM($data, 1, null));
	}

	function welcome_message_template_action()
	{
		if ($this->user_info['birthday']) //默认的信息
		{
			TPL::assign('birthday_y_s', date('Y', $this->user_info["birthday"]));
			TPL::assign('birthday_m_s', date('m', $this->user_info["birthday"]));
			TPL::assign('birthday_d_s', date('d', $this->user_info["birthday"]));
		}
		
		//年符值
		$year_end = date("Y") * 1;
		$year_array[0] = "";
		
		for ($tmp_i = $year_end; $tmp_i > 1900; $tmp_i --)
		{
			$year_array[$tmp_i] = $tmp_i;
		}
		
		TPL::assign('birthday_y', $year_array);
		
		//月符值
		TPL::assign('birthday_m', array(
			"0" => "", 
			"01" => "01", 
			"02" => "02", 
			"03" => "03", 
			"04" => "04", 
			"05" => "05", 
			"06" => "06", 
			"07" => "07", 
			"08" => "08", 
			"09" => "09", 
			"10" => "10", 
			"11" => "11", 
			"12" => "12"
		));
		
		//日符值
		

		$day_array[0] = '';
		
		for ($tmp_i = 1; $tmp_i <= 31; $tmp_i ++)
		{
			$day_array[$tmp_i] = $tmp_i;
		}
		
		TPL::assign('birthday_d', $day_array);
		
		TPL::output('account/ajax/welcome_message_template');
	}

	function welcome_get_questions_action()
	{
		if( $questions_list = $this->model('question')->get_questions(5, 'rand()'))
		{
			foreach ($questions_list as $key => $question_info)
			{
				$questions_list[$key]['focus'] = $this->model('question')->has_focus_question($question_info['question_id'], $this->user_id);
			}
		}
		TPL::assign('questions_list', $questions_list);
		
		TPL::output('account/ajax/welcome_get_questions_ajax');
	}

	function welcome_get_topics_action()
	{
		if ($topics_list = $this->model('topic')->get_topic_list_v2(12, 'rand()'))
		{
			foreach ($topics_list as $key => $topic)
			{
				$topics_list[$key]['focus'] = $this->model('topic')->has_focus_topic($this->user_id, $topic['topic_id']);
			}
		}
		TPL::assign('topics_list', $topics_list);
		
		TPL::output('account/ajax/welcome_get_topics_ajax');
	}

	function welcome_get_users_action()
	{
		if($users_list = $this->model('account')->get_activity_random_users(6))
		{
			foreach ($users_list as $key => $val)
			{
				$user_job = $this->model("account")->get_user_jobs_by_uids($val['uid']);
				
				$users_list[$key]['user_job'] = $user_job[$val['uid']];
				$users_list[$key]['focus'] = $this->model('follow')->user_follow_check($this->user_id, $val['uid']);
			}
		}
		TPL::assign('users_list', $users_list);
		
		TPL::output('account/ajax/welcome_get_users_ajax');
	}

	function clean_first_login_action()
	{
		$this->model('account')->clean_first_login($this->user_id);
		
		die('success');
	}

	function update_signature_action()
	{
		if (! $this->user_id)
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '请先登录'));
		}
		
		$this->model('account')->update_signature($this->user_id, $_POST['signature']);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, '签名更新成功'));
	}

	/**
	 * 微博分享
	 */
	function weibo_push_action()
	{
		if (! $this->user_id)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'share_send'
			), '-1', '请先登录'));
		}
		
		if (! $_POST['push_message'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'share_send'
			), '-1', '请输入分享内容'));
		}
		
		if (! $_POST['push_qq'] and ! $_POST['push_sina'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'share_send'
			), '-1', '请选择要分享的微博'));
		}
		
		if ($_POST['push_qq'] AND get_setting('qq_t_enabled') == 'Y')
		{
			if ($openid_info = $this->model('qq_weibo')->get_users_qq_by_uid($this->user_id))
			{
				Services_Tencent_OpenSDK_Tencent_Weibo::init(get_setting('qq_app_key'), get_setting('qq_app_secret'));
				
				$result = Services_Tencent_OpenSDK_Tencent_Weibo::call('t/add', array(
					'content' => htmlspecialchars($_POST['push_message'])
				), 'POST');
				
				if ($result['errcode'])
				{
					H::ajax_json_output(AWS_APP::RSM(array(
						'tips_id' => 'share_send'
					), '-1', "QQ 微博发送失败，错误代码: {$result['errcode']}, 信息：{$result['msg']}"));
				}
				/*else
				{
					//分享成功,加积分
					$this->model("integral")->set_user_integral("SHARE", $this->user_id, "腾讯微博分享成功");
				}*/
			}
		}
		
		if ($_POST['push_sina'] AND get_setting('sina_weibo_enabled') == 'Y')
		{
			if ($openid_info = $this->model('sina_weibo')->get_users_sina_by_uid($this->user_id))
			{
				//$client = new Services_Weibo_WeiboClient(get_setting('sina_akey'), get_setting('sina_skey'), $openid_info['oauth_token'], $openid_info['oauth_token_secret']);
				$client = new Services_Weibo_WeiboClient(get_setting('sina_akey'), get_setting('sina_skey'), $openid_info['access_token']);
				
				$result = $client->update(htmlspecialchars($_POST['push_message'])); //发送微博
				
				if ($result['error_code'])
				{
					H::ajax_json_output(AWS_APP::RSM(array(
						'tips_id' => 'share_send'
					), '-1', "新浪微博发送失败，错误：{$result['error_code']}: {$result['error']}"));
				}
				/*else
				{
					//分享成功,加积分
					//$this->model("integral")->set_user_integral("SHARE", $this->user_id, "新浪微博分享成功");
				}*/
			}
		}
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, '分享发送成功'));
	}

	public function send_invite_question_mail_action()
	{
		if (! $this->user_id)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'share_send'
			), '-1', '请先登录'));
		}
		
		if (! $_POST['email_message'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'share_send'
			), '-1', '请输入邮件内容'));
		}
		
		if (! H::isemail($_POST['email_address']))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'share_send'
			), '-1', '邮件地址格式错误'));
		}
		
		if($_POST['email_address'] == $this->user_info['email'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'share_send'
			), '-1', '不能发送给自己'));
		}
		
		//$invite_url = get_js_url('/account/register/step1/invite_question_id-' . (int)$_POST['question_id'] . '__source-' . urlencode($this->model('account')->get_source_hash($this->user_info['email'])));
		
		//$question_info = $this->model('question')->get_question_info_by_id($_POST['question_id']);
		
		$this->model('email')->send($_POST['email_address'], $this->user_info['user_name'] . ' 邀请你来 ' . get_setting('site_name') . ' 回复问题', nl2br($_POST['email_message']));
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, '邀请发送成功'));
	}
	
	public function get_draft_action()
	{
		H::ajax_json_output($this->model('draft')->get_data($_GET['item_id'], $_GET['type'], $this->user_id));
	}
	
	public function delete_draft_action()
	{
		if (!$_POST['item_id'] OR !$_POST['type'])
		{
			die;
		}
		
		$this->model('draft')->delete_draft($_POST['item_id'], $_POST['type'], $this->user_id);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, '草稿删除成功'));
	}
	
	public function save_draft_action()
	{
		if (!$_GET['item_id'] OR !$_GET['type'] OR !$_POST)
		{
			die;
		}
		
		$this->model('draft')->save_draft($_GET['item_id'], $_GET['type'], $this->user_id, $_POST);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, '已保存草稿，' . date('Y-m-d H:i:s', time())));
	}
	
	/**
	 * 不感兴趣的话题
	 */
	public function uninterested_topic_action()
	{
		$topic_id = intval($_GET['topic_id']);
			
		if (!$topic_info = $this->model('topic')->get_topic($topic_id))
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', "话题不存在"));
		}
		
		if ($this->model('topic')->save_topic_uninterested($this->user_id, $topic_id))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "1", "操作成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', "操作失败, 请联系管理员"));
		}
	}

	/**
	 * 不感兴趣的人
	 */
	public function uninterested_user_action()
	{
		$user_id = intval($_GET['user_id']);	
		
		if (!$user_info = $this->model('account')->get_users_by_uid($user_id))
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', "用户不存在"));
		}
		
		if ($this->model('account')->save_user_uninterested($this->user_id, $user_id))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "1", "操作成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', "操作失败, 请联系管理员"));
		}
	}
	
	function send_valid_mail_action()
	{
		if (!$this->user_id)
		{
			if ( H::isemail($_SESSION['valid_email']))
			{
				$this->user_info = $this->model('account')->get_users_by_email($_SESSION['valid_email']);
				$this->user_id = $this->user_info['uid'];
			}
		}
		
		if (! H::isemail($this->user_info['email']))
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '没有设置 E-mail'));
		}
		
		if ($this->user_info['valid_email'] == 1)
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', "邮箱已经认证"));
		}
		
		if ($this->model('active')->new_valid_email($this->user_id))
		{
			H::ajax_json_output(AWS_APP::RSM(null, 1, "验证邮件发送成功,请去收件箱收取邮箱进行认证")); // 返回数据
		}
	}
	
	function valid_email_active_action()
	{
		$active_code = $_POST['active_code'];
		$email = $_POST['email'];
		$password = $_POST['password'];
				
		$active_code_row = $this->model('active')->get_active_code_row($active_code, 21);
		
		if (!$active_code_row)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => get_js_url('/'),
			), 1, '激活失败, 链接已无效'));
		}
		
		if ($active_code_row["active_time"] || $active_code_row["active_ip"] || $active_code_row["active_expire"])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => get_js_url('/account/login/'),
			), 1, '帐户已激活, 请返回登录'));
		}

		if (!$user_info = $this->model('account')->check_login($email, $password))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
			), -1, '密码错误'));
		}
		
		if ($user_info['uid'] != $active_code_row['uid'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => get_js_url('/account/login/'),
			), 1, '请使用注册的邮箱及密码激活帐户'));
		}
		
		if ($user_info['valid_email'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => get_js_url('/account/login/'),
			), 1, '帐户已通过邮箱验证，请返回登录'));
		}
		
		$user_id = $this->model('active')->active_code_active($active_code, 21);

		if ($user_id)
		{
			if ($_SESSION['valid_email'])
			{
				unset($_SESSION['valid_email']);
			}
			
			$this->model('account')->update_users_fields(array(
				'valid_email' => 1,
			), $active_code_row['uid']);
			
			if($user_info['group_id'] == 3)
			{
				$this->model('account')->update_users_fields(array(
					'group_id' => 4,
				), $active_code_row['uid']);
			}
			
			//帐户激活成功，切换为登录状态跳转至首页

			$this->model('account')->setsession_logout();
			
			$this->model('account')->setcookie_logout();
			
			$this->model('account')->update_user_last_login($user_info['uid']);
				
			HTTP::set_cookie('r_uname', $email, time() + 60 * 60 * 24 * 30);
				
			$this->model('account')->setcookie_login($user_info['uid'], $email, $password, $user_info['salt']);
			
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => get_js_url('/'),
			), 1, '帐户激活成功'));
		}
	}
	
	function request_find_password_action()
	{
		if (!H::isemail($_POST['email']))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => "email"
			), "-1", "请填写正确的邮箱地址"));
		}		

		if (!$user_info = $this->model('account')->get_users_by_email($_POST['email']))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => "email"
			), "-1", "邮箱地址错误或帐号不存在。"));
		}
		
		if (!core_captcha::validate($_POST['seccode_verify'], false))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => "seccode_verify"
			), "-1", "请填写正确的验证码"));
		}
		
		core_captcha::clear();
		
		$this->model('active')->new_find_password($user_info["uid"]); //发送激活邮件
		
		$_SESSION['find_password'] = $_POST['email'];
		
		H::ajax_json_output(AWS_APP::RSM(array(
			'url' => get_js_url('/account/find_password/process_success/')
		), "1", null));
	}
	
	function find_password_modify_action()
	{
		if (!core_captcha::validate($_POST['seccode_verify'], false))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => "seccode_verify"
			), "-1", "请填写正确的验证码"));
		}
		
		core_captcha::clear();
		
		$active_code = $_POST['active_code'];
		
		$active_code_row = $this->model('active')->get_active_code_row($active_code, 11);
		
		if ($active_code_row)
		{
			if ($active_code_row["active_time"] || $active_code_row["active_ip"] || $active_code_row["active_expire"])
			{
				H::ajax_json_output(AWS_APP::RSM(null, "1", "链接已失效，请重新找回密码"));
			}
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, "1", "链接已失效，请重新找回密码"));
		}
		
		if (empty($_POST['password']))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => "password"
			), "-1", "密码不能为空"));
		}

		//检查验证码
		if ($_POST['password'] != $_POST['re_password'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => "password"
			), "-1", "两次输入的密码不一致"));
		}
		
		//激活代码检查成功
		$uid = $this->model('active')->active_code_active($active_code, 11);
		
		if (! $uid)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "找回密码 Key 已失败")); //返回数据
		}
		
		$user_info = $this->model('account')->get_users_by_uid($uid);
		
		if ($this->model('account')->update_user_password_ingore_oldpassword($_POST['password'], $uid, $user_info['salt']))
		{
			//若账户未激活，则激活账户
			$this->model('account')->update_users_fields(array(
				'valid_email' => 1
			), $uid);
			
			if($user_info['group_id'] == 3)
			{
				$this->model('account')->update_users_fields(array(
					'group_id' => 4,
				), $active_code_row['uid']);
			}
			
			$this->model("account")->setcookie_logout(); //清除COOKIE
			
			$this->model("account")->setsession_logout(); //清除session;
			
			$_SESSION["error"] = 0; //重置登录出错计数
			
			unset($_SESSION['find_password']);

			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => get_js_url('/account/login/'),
			), "1", "密码修改成功, 请返回登录"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "修改失败, 请联系管理员"));
		}
	}
}